<?php
class ControllerCommonForgotten extends Controller {
	public function index() {
		$this->document->setTitle("forgotten");
		$data['sendmailforpassword'] = $this->url->link('common/forgotten/sendmailforpassword','',true);
		$data['loginurl'] = $this->url->link('common/login', '', true);
		$data['header'] = $this->load->controller('common/headerout');
 		$data['footerout'] = $this->load->controller('common/footerout');
 		$data['footer'] = $this->load->controller('common/footer');
 		$this->response->setOutput($this->load->view('common/forgotten', $data));
	}
	public function sendmailforpassword() {
 		$this->load->model('user/user');
 		if (!$this->model_user_user->getTotalUsers($this->request->post['username'])) {
 			header('Content-Type:application/json; charset=utf-8');
            exit(json_encode(1,0));
 		}
 		if (!$this->model_user_user->getTotalUsersByEmail($this->request->post['email'])) {
 			header('Content-Type:application/json; charset=utf-8');
            exit(json_encode(2,0));
 		}
 		$username = $this->request->post['username'];
 		$email = $this->request->post['email'];
 		$result = $this->model_user_user->getUseridByEmail($email);
 		$userid = $result['user_id'];
 		$salt = token(9);
 		$saltanduserid = $salt."-".$userid;
 		$this->cache->set($userid."sandu",$saltanduserid);
 		$this->cache->set($userid."salt",$salt); 		
		$token = sha1($salt . sha1($salt . sha1($saltanduserid)));
		$url = $this->url->link('common/resetpassword', '', true);
		$url .= "&forgottenid=".$userid."&token=".$token;
		//发送密码重置邮件开始
		$this->load->library("email");
		$smtpemailto = $email;//发送给谁
		$mailtitle = "memory-curve密码重置邮件请勿回复";//邮件主题
		$mailcontent = "请<a href='".$url."'>点击链接</a>以重置您的账号密码！";//邮件内容

		$smtp = new email($smtpemailto,$mailtitle,$mailcontent);
		$state = $smtp->sendemail();
 		header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($state,0));
 	}
}